<?php
/**
 * 公司
 * @date    2020-01-01
 * @author  kiro
 * @email   294843009@qq.com
 * @version 1.0
 */
namespace App\Http\Controllers\Api\V1\Admin;

use App\Models\Company;
use Illuminate\Http\Request;

class CompanyController extends BaseController
{
    public function index()
    {
        $type = request()->input('type');
        $status = request()->input('status');
        $list = Company::where(function($query) use ($type, $status) {
                if ($type) {
                    if (is_array($type)) {
                        $query->whereIn('type', $type);
                    } else {
                        $query->where('type', $type);
                    }
                }
                if ($status) {
                    $query->where('status', $status);
                } else {
                    $query->whereIn('status', [1, 2]);
                }
            })->orderBy('sort', 'ASC')
            ->orderBy('id', 'DESC')
            ->paginate($this->page_size);
        foreach($list as &$value){
            $value['thumb_url'] = dealUrl($value['thumb']);
            $images = array_filter(explode(',', $value['images']));
            $image_list = [];
            foreach ($images as $image) {
                $image_list[] = dealUrl($image);
            }
            $value['images'] = $images;
            $value['images_list'] = $image_list;
        }
        return $this->success('成功', $list);
    }


    public function store(Request $request)
    {
        if (empty($request->input('name'))) {
            return $this->error('公司名称不能为空');
        }
        $model = new Company();
        $request->name AND $model->name = htmlspecialchars($request->name);
        $request->type AND $model->type = $request->type;
        $request->thumb AND $model->thumb = $request->thumb;
        $request->tel AND $model->tel = $request->tel;
        $request->address AND $model->address = $request->address;
        $request->legal_person AND $model->legal_person = $request->legal_person;
        $request->desc AND $model->desc = htmlspecialchars($request->desc);
        $request->sort AND $model->sort = $request->sort;
        if (!empty($request->images) && count($request->images) && is_array($request->images)) {
            $model->images = implode(',', $request->images);
        }
        $model->status = 1;
        $result = $model->save();
        if ($result) {
            return $this->success('添加成功');
        } else {
            return $this->error('添加失败');
        }
    }

    public function update(Request $request, $id)
    {
        if (empty($request->input('name'))) {
            return $this->error('公司名称不能为空');
        }
        $model = Company::find($id);
        $request->name AND $model->name = htmlspecialchars($request->name);
        $request->type AND $model->type = $request->type;
        $request->thumb AND $model->thumb = $request->thumb;
        $request->tel AND $model->tel = $request->tel;
        $request->address AND $model->address = $request->address;
        $request->legal_person AND $model->legal_person = $request->legal_person;
        $request->desc AND $model->desc = htmlspecialchars($request->desc);
        $request->sort AND $model->sort = $request->sort;
        if (!empty($request->images) && count($request->images) && is_array($request->images)) {
            $model->images = implode(',', $request->images);
        }
        $model->status = 1;
        $result = $model->save();
        if ($result) {
            return $this->success('修改成功', $result);
        } else {
            return $this->error('修改失败');
        }
    }

    public function destroy($id)
    {
        $result = Company::where('id', $id)->update(['status' => 0]);
        if ($result) {
            return $this->success('删除成功', $result);
        } else {
            return $this->error('删除失败');
        }
    }

    public function batchDisable(Request $request)
    {
        $input = $request->toArray();
        if (!in_array($input['status'], [0, 1, 2])) {
            return $this->error('状态不正确');
        }
        $result = Company::whereIn('id', $input['selection'])->update(['status' => $input['status']]);
        if ($result) {
            return $this->success('保存成功');
        } else {
            return $this->error('保存失败');
        }
    }
}
